package selfview.sufutian.com.myapplication.animi;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by sufutian on 2017/1/12.
 */

public class WaveTest extends View {
    /**
     * 画线的画笔
     */
    private Paint linePaint;
    /**
     * 填充的画笔
     */
    private Paint fillPaint;
    /**
     * 边框
     */
    private Path circlePath;
    /**
     * 波浪路径
     */
    private Path wavePath;

    public WaveTest(Context context, AttributeSet attrs) {
        super(context, attrs);

        initPaint();
    }

    private void initPaint() {

        linePaint = new Paint();
        fillPaint = new Paint();

        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(10);
        linePaint.setColor(Color.RED);

        fillPaint.setStyle(Paint.Style.FILL);
        fillPaint.setColor(Color.RED);

        circlePath = new Path();

        wavePath = new Path();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    /**
     * 一条波浪长度
     */
    int waveWidth = 400;
    /**
     * 波浪高度
     */
    int waveHeight = 25;
    /**
     *
     */
    int waveCount = 3;

    /**
     * 横向的偏移量
     */
    int offsetHorizontal = 10;
    /**
     * 每次上升的高度
     */
    int offsetVertical = 1;
    /**
     * 绘制的间隙:毫秒
     */
    int looperTime = 16;

    /**
     * path开始x
     */
    int startX = -waveWidth;

    int moveCount_X;

    /**
     * 水位上升时不断变化的 y 坐标
     */
    private float mChangeY = 800;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        wavePath.reset();
        circlePath.reset();

        if (canvas.isHardwareAccelerated() || this.isHardwareAccelerated()) {//关闭硬件加速
            this.setLayerType(LAYER_TYPE_SOFTWARE, null);
        }

        circlePath.addCircle(400, 400, 400, Path.Direction.CW);
        canvas.clipPath(circlePath);//裁剪 只显示圆形的区域
        canvas.drawPath(circlePath, linePaint); //画出圆


        wavePath.moveTo(startX, mChangeY);//波浪起点


        for (int i = 0; i < waveCount; i++) {//一条横向上绘制waveCount个周期波浪
            wavePath.quadTo(startX + waveWidth / 4 + (i * waveWidth) + moveCount_X, mChangeY + waveHeight, startX + waveWidth / 2 + (i * waveWidth) + moveCount_X, mChangeY);

            wavePath.quadTo(startX + waveWidth / 4 * 3 + (i * waveWidth) + moveCount_X, mChangeY - waveHeight, startX + waveWidth + (i * waveWidth) + moveCount_X, mChangeY);
        }

        mChangeY -= offsetVertical;  //y值向上
        moveCount_X += offsetHorizontal;

        if (moveCount_X >= waveWidth) moveCount_X = 0;//一个周期 从头开始
        if (mChangeY <= 200) {
            mChangeY = 200;
        }

        wavePath.lineTo(800, 800);//画路径
        wavePath.lineTo(0, 800);//终点
        wavePath.close();
        canvas.drawPath(wavePath, fillPaint);


        postInvalidateDelayed(looperTime);


    }


}
